package cn.edu.uestc.software.service;

import cn.edu.uestc.software.mapper.UserMapper;
import cn.edu.uestc.software.pojo.Role;
import cn.edu.uestc.software.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Map<String, Object> selectUsers(int page,
                                           String roleName,
                                           String roleLevel,
                                           String userName,
                                           String departmentName) {
        int index = (page - 1) * 10;                   // 根据页码计算出当前页的第一条数据下标
        Map<String, Object> map = new HashMap<>();
        List<User> users = userMapper.selectUsers(index,roleName,roleLevel,userName,departmentName);    // 通过sql获取数据库pojo数据
        int counts = userMapper.selectUserCounts(roleName,roleLevel,userName,departmentName);            // 通过sql获取总条数
        map.put("code", 0);           // layui默认返回0的状态为成功
        map.put("msg", "");           // layui不解析msg中的信息
        map.put("count", counts);     // 记录数据表中总条数
        map.put("data", users);       // 当前页的数据内容
        return map;
    }

    public Map<String,Object> Update(User user){
        String password=user.getPassword();
        String password_md5= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        String password_md5_02= DigestUtils.md5DigestAsHex(password_md5.getBytes(StandardCharsets.UTF_8));
        user.setPassword(password_md5_02);
        userMapper.Update(user);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","更新成功");
        return map;
    }

    public Map<String,Object> SelsctUserById(String id){
        User userById = userMapper.SelectUsersById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",userById);
        return map;
    }

    public Map<String,Object> DeleteById(String id){
        userMapper.DeleteById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","删除成功");
        return map;
    }

    public Map<String,Object> InsertUser(String username,String password){
        userMapper.InsertUser(username, password);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","新增成功");
        return map;
    }

    public Map<String,Object> InsertUserJSON( User user){
        System.out.println("新增用户之前  id="+user.getId());
        String password=user.getPassword();
        String password_md5= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        String password_md5_02= DigestUtils.md5DigestAsHex(password_md5.getBytes(StandardCharsets.UTF_8));
        user.setPassword(password_md5_02);
        userMapper.InsertUserJSON(user);
        System.out.println("新增用户之后  id="+user.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","新增成功");
        return map;
    }

    public Map<String, Object> login(User user, HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();

        User user_result = userMapper.login(user.getUsername());

        if(user_result == null) {
            map.put("code", 901);
            map.put("msg", "用户名不存在");
            return map;
        }

        String password = user.getPassword();
        String password_md5 = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        String password_md5_02 = DigestUtils.md5DigestAsHex(password_md5.getBytes(StandardCharsets.UTF_8));
        if(!user_result.getPassword().equals(password_md5_02)) {
            map.put("code", 902);
            map.put("msg", "对不起，密码错误");
            return map;
        }

        map.put("code", 200);
        map.put("msg", "恭喜您，登录成功");
        request.getSession().setAttribute("user", user);
        return map;
    }
}
